package org.caike.lab.excel.controller;

import org.caike.lab.excel.domian.ExcelData;
import org.caike.lab.excel.utils.ExportExcelUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
public class ExcelController {

    @RequestMapping(value = "/excel", method = RequestMethod.GET)
    public void excel(HttpServletResponse response) throws Exception {
        List<ExcelData> datas = new ArrayList();

        ExcelData data = new ExcelData();
        data.setName("hello");
        List<String> titles = new ArrayList();
        titles.add("a1");
        titles.add("a2");
        titles.add("a3");
        data.setTitles(titles);

        List<List<Object>> rows = new ArrayList();
        List<Object> row = new ArrayList();
        row.add("11111111111");
        row.add("22222222222");
        row.add("3333333333");
        rows.add(row);
        data.setRows(rows);

        ExcelData data1 = new ExcelData();
        data1.setName("hello1");
        List<String> titles1 = new ArrayList();
        titles1.add("a11");
        titles1.add("a21");
        titles1.add("a31");
        data1.setTitles(titles1);

        List<List<Object>> rows1 = new ArrayList();
        List<Object> row1 = new ArrayList();
        row1.add("11111111111");
        row1.add("22222222222");
        row1.add("3333333333");
        rows1.add(row1);
        data1.setRows(rows1);

        datas.add(data);
        datas.add(data1);

        ExportExcelUtils.exportExcel(response, "hello.xlsx", datas);
    }
}